//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using Microsoft.Practices.Mobile.PasswordAuthentication;
using System.Xml;

namespace ConfigSectionEncrypt
{
	public partial class SectionEncrypt : Form
	{
		public SectionEncrypt()
		{
			InitializeComponent();

			GenerateNewKey();
		}

		private void EncryptSectionButton_Click(object sender, EventArgs e)
		{
			try
			{
				EncryptedTextBox.Text = CryptoTasks.EncryptSection(OriginalTextBox.Text, Key);
				SectionTabControl.SelectedTab = EncryptedTabPage;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void DecryptSectionButton_Click(object sender, EventArgs e)
		{
			try
			{
			OriginalTextBox.Text = CryptoTasks.DecryptSection(EncryptedTextBox.Text, Key);
			SectionTabControl.SelectedTab = SectionTabPage;
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

		private void NewKeyButton_Click(object sender, EventArgs e)
		{
			GenerateNewKey();
		}

		private byte[] Key
		{
			get { return Convert.FromBase64String(KeyTextBox.Text); }
		}

		private void GenerateNewKey()
		{
			KeyTextBox.Text = Convert.ToBase64String(CryptoTasks.GenerateKey());
		}

		private void EncryptKeyButton_Click(object sender, EventArgs e)
		{
			try
			{
				EncryptedKeyTextBox.Text = Convert.ToBase64String(CryptoTasks.EncryptKey("notused", PasswordTextBox.Text, Key));
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void DecryptKeyButton_Click(object sender, EventArgs e)
		{
			try
			{
			byte[] encryptedKey = Convert.FromBase64String(EncryptedKeyTextBox.Text);
			byte[] key = CryptoTasks.DecryptKey("notused", PasswordTextBox.Text, encryptedKey);
			KeyTextBox.Text = Convert.ToBase64String(key);
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

		private void EncryptedKeyTextBox_TextChanged(object sender, EventArgs e)
		{
			DecryptKeyButton.Enabled = !String.IsNullOrEmpty(EncryptedKeyTextBox.Text);
		}

		private void OriginalTextBox_TextChanged(object sender, EventArgs e)
		{
			EncryptSectionButton.Enabled = !String.IsNullOrEmpty(OriginalTextBox.Text);
		}

		private void EncryptedTextBox_TextChanged(object sender, EventArgs e)
		{
			DecryptSectionButton.Enabled = !String.IsNullOrEmpty(EncryptedTextBox.Text);
		}

		private void PasswordTextBox_TextChanged(object sender, EventArgs e)
		{
			EncryptKeyButton.Enabled = !String.IsNullOrEmpty(PasswordTextBox.Text);
		}
	}
}